package com.qidian.server.socket;


import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.net.Socket;

import org.apache.log4j.Logger;

import com.qidian.common.Constants;
import com.qidian.server.MServer;
import com.qidian.server.Status;
import com.qidian.util.ByteUtil;


public class OthersWorker implements Runnable{

    protected Socket clientSocket = null;
    protected String serverText   = null;
	protected byte[] buf = new byte[Constants.socket_buf_size];
	protected MServer mserver = MServer.getInstance() ;
	
    static Logger logger = Logger.getLogger(OthersWorker.class) ;
    
    public OthersWorker(Socket clientSocket, String serverText) {
        this.clientSocket = clientSocket;
        this.serverText   = serverText;
    }

    public void run() 
    {
    	Status status = Status.OK ;
    	try 
        {
    		// 设置Socket超时时间
    		clientSocket.setSoTimeout(Constants.socket_time_out) ;
    		
            InputStream input  = clientSocket.getInputStream();
            OutputStream output = clientSocket.getOutputStream();
            int readSize = 0 ;
            
            // 发送一个随机校验字节
            byte randomByte = (byte)(Math.random()*255+1) ;
            output.write(randomByte) ;
//            System.out.println("Random seed:"+randomByte) ;
            int backRandomByte = input.read() ;
//            System.out.println("Back random seed:"+(byte)backRandomByte) ;
            if( (byte)backRandomByte !=  randomByte)
            {
            	input.close() ;
            	output.close() ;
            	return ;
            }
            
            // 开始处理正式消息
            readSize = input.read(buf);
            int byteArgLen = ByteUtil.readInt(buf, 0) ;
            String strArgs = new String(buf,4,byteArgLen);
            String[] args= strArgs.split(Constants.splitString);
            
            if (args == null || args.length < 4)
            	status = Status.INVALID_ARGUMENT ;
            else
            {
            	String account = args[0] ;
            	String contentType = args[1] ;
            	String pGameType = args[2] ;
            	String pInterval = args[3] ;
            	String fromIP = clientSocket.getRemoteSocketAddress().toString() ;
            	fromIP = fromIP.substring(1,fromIP.length()) ;
            	int fromPort = -1 ;
            	byte[] content = new byte[readSize-4-byteArgLen] ;
            	System.arraycopy(buf, 4+byteArgLen, content, 0, content.length) ;
            	int gameType = Integer.parseInt(pGameType) ;
            	int interval = Integer.parseInt(pInterval) ;
            	StringBuffer result = new StringBuffer(32) ;
//            	status = mserver.p2s_sendFile(contentType, content,
//            			 account, gameType, interval, fromIP, fromPort, result);
            	if(status == Status.OK)
            		output.write(result.toString().getBytes()) ;
            	else
            		output.write(status.toString().getBytes()) ;
            }
            
            output.close();
            input.close();
        }
        catch (Exception e) 
        {
            e.printStackTrace();
            logger.error(e) ;
        }
        finally{
        	try {
				clientSocket.close() ;
			} catch (IOException e) {
			}
        }
    }
    private void sendFile(String args[],InputStream is, OutputStream os)
    {

    }
}